<template>
    <el-form :model="form"
             :rules="rules"
             ref="formRef">
        <el-form-item label="代理名称"
                      prop="name"
                      :label-width="formLabelWidth">
            <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="序号"
                      prop="sort"
                      :label-width="formLabelWidth">
            <el-input v-model.number="form.sort"
                      placeholder="请填写数字"></el-input>
        </el-form-item>
        <el-form-item label="代理准入标准(单位)"
                      prop="num"
                      :label-width="formLabelWidth">
            <el-input v-model.number="form.num"
                      placeholder="请填写数字"></el-input>
        </el-form-item>
        <el-form-item label="代理返现额度(%)"
                      prop="rebateProportion"
                      :label-width="formLabelWidth">
            <el-input v-model.number="form.rebateProportion"
                      placeholder="请填写数字"></el-input>
        </el-form-item>
        <!-- <el-form-item label="是否自动升级" prop="autoUpdate" :label-width="formLabelWidth">
      <el-select v-model="form.autoUpdate" placeholder="请选择" disabled>
        <el-option label="是" value="1"></el-option>
        <el-option label="否" value="0"></el-option>
      </el-select>
    </el-form-item>-->

        <el-form-item :label-width="formLabelWidth">
            <el-button plain
                       @click="resetForm('formRef')">取消</el-button>
            <el-button type="primary"
                       @click="submitForm('formRef')">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    data() {
        return {
            form: {
                name: "",
                sort: "",
                num: "",
                rebateProportion: "",
                autoUpdate: "1"
            },
            formLabelWidth: "150px",
            rules: {
                name: [
                    { required: true, message: "请输入姓名", trigger: "blur" },
                    { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
                ],
                sort: [
                    { required: true, message: "请输入序号", trigger: "blur" },
                    { type: "number", message: "序号必须为数字值" }
                ],
                num: [
                    { required: true, message: "请输入代理准入标准", trigger: "blur" },
                    { type: "number", message: "请输入数字" }
                ],
                rebateProportion: [
                    { required: true, message: "代理返现额度(%)", trigger: "blur" },
                    { type: "number", message: "请输入数字" }
                ]
                // autoUpdate: [
                //   { required: true, message: "代理返现额度(%)", trigger: "blur" }
                // ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$service
                        .addAgency({
                            name: this.form.name,
                            remark: "",
                            num: this.form.num,
                            sort: this.form.sort,
                            rebateProportion: this.form.rebateProportion
                        })
                        .then(res => {
                            //console.log(res);
                            if (res.code == '00000') {
                                this.$message({
                                    message: "添加成功",
                                    type: "success"
                                });
                                this.$refs[formName].resetFields();
                                this.$emit("submit", false);
                            } else {
                                this.$message({
                                    message: res.message,
                                    type: "warning"
                                });
                            }
                        });
                }
            });
        },
        resetForm(formName) {
            this.$emit("cancel", false);
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>